package com.dream.technology.controller;


import com.dream.technology.data.GenericRspDTO;
import com.dream.technology.data.NoBody;
import com.dream.technology.db.entity.RcRule;
import com.dream.technology.db.entity.RcRuleItem;
import com.dream.technology.db.entity.RcRuleQuery;
import com.dream.technology.db.entity.vo.VoRule;
import com.dream.technology.service.RcRuleItemService;
import com.dream.technology.service.RcRuleService;
import com.dream.technology.vo.ErrorCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/risk/admin/rule")
public class RcRuleController {

    private static final Logger logger = LoggerFactory.getLogger(RcRuleController.class);

    @Autowired
    RcRuleService rcRuleService;
    @Autowired
    RcRuleItemService rcRuleItemService;

    /**
     * 风控控规则接口
     *
     * @param pageNum
     * @param pageSize
     * @param classifyId
     * @param ruleCode
     * @param ruleName
     * @return
     */
    @GetMapping("/list")
    public GenericRspDTO<Object> getList(Integer pageNum, Integer pageSize, String ruleName, Integer ruleTypeId) {

        RcRuleQuery query = new RcRuleQuery();
        RcRuleQuery.Criteria criteria1 = query.createCriteria();
        RcRuleQuery.Criteria criteria2 = query.createCriteria();
        criteria1.andStatusEqualTo(false);
        if (ruleTypeId != null) {
            criteria1.andClassifyIdEqualTo(ruleTypeId);
        }
        if (StringUtils.isNotBlank(ruleName)) {
            criteria1.andRuleNameLike("%" + ruleName + "%");
            criteria2.andRuleCodeLike("%" + ruleName + "%");
        }
        query.or(criteria2);
        query.setOrderByClause("enabled desc,create_date desc");
        PageHelper.startPage(pageNum, pageSize);
        List<RcRule> rcRuleList = rcRuleService.selectByExample(query);
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), new PageInfo<RcRule>(rcRuleList));
    }

    /**
     * 风控规则回显接口
     *
     * @param id
     * @return
     */
    @GetMapping("/id")
    public GenericRspDTO<VoRule> getRcRuleById(Long id) {

        RcRule rcRule = rcRuleService.selectByPrimaryKey(id);
        List<RcRuleItem> rcRuleItems = rcRuleItemService.selectByRuleId(id);
        VoRule rule = new VoRule();
        rule.setRcRule(rcRule);
        rule.setRcRuleItem(rcRuleItems);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), rule);
    }

    /**
     * 添加风控规则
     *
     * @param rcRule
     * @return
     */
    @PostMapping("/add")
    public GenericRspDTO<Object> addRcRule(@RequestBody VoRule rcRule) {

        String result = rcRuleService.insertStr(rcRule);
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), result);
    }


    /**
     * 修改风控规则
     *
     * @param rcRule
     * @return
     */
    @PostMapping("/update")
    public GenericRspDTO<Object> updateRcRule(@RequestBody VoRule rcRule) {

        rcRuleService.updateByJsonStr(rcRule);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), null);
    }

    /**-
     * 删除或停用风控规则 (逻辑删除)
     * @param id	规则ID
     * @param status	是否删除：1删除，0不删除
     * @param enabled	是否启用：1启用，0不启用
     * @return
     */
    @PostMapping("/deleteOrStop")
    public GenericRspDTO<NoBody> deleteRcRule(Long id, Boolean status, Boolean enabled) {
        RcRule rule = new RcRule();
        rule.setId(id);
        if (status != null) {
        	rule.setStatus(status);
		}
		if (enabled != null) {
			rule.setEnabled(enabled);
		}
        rcRuleService.updateByPrimaryKeySelective(rule);
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), null);
    }

    /**
     * 风控规则集验证
     *
     * @param
     * @return
     */
    @PostMapping("/checkForDoc")
    public GenericRspDTO<Object> checkRcCustomerTag(@RequestBody VoRule rule) {
        String result = rcRuleService.checkRule(rule);
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), result);
    }

    /**
     * 风控规则集验证的引用
     *
     * @param
     * @return
     */
    @PostMapping("/checkRef")
    public GenericRspDTO<Object> checkRef(Long id) {
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), rcRuleService.checkRuleRef(id));
    }


    /**
     *  复制规则
     * @param id
     * @param name
     * @param code
     * @return
     */
    @PostMapping("/copy")
    public GenericRspDTO<Object> copy(Long id, String ruleName, String ruleDetail, Boolean enabled) {

        rcRuleService.copy(id, ruleName, ruleDetail, enabled);
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), "");
    }
    
    /**
     * 获取所有可用的规则
     * @return
     */
    @GetMapping("/ruleList")
    public GenericRspDTO<Object> getRuleList(){
        RcRuleQuery rcRuleQuery = new RcRuleQuery();
        RcRuleQuery.Criteria criteria = rcRuleQuery.createCriteria();
        criteria.andStatusEqualTo(false);
        criteria.andEnabledEqualTo(true);
        List<RcRule> rcRuleList = rcRuleService.selectByExample(rcRuleQuery);
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), rcRuleList);
    }
    
    /**
     * 验证名称是否重复
     * @param name
     * @return
     */
    @GetMapping("/getByName")
    public GenericRspDTO<Object> getByName(String name){
    	RcRuleQuery query = new RcRuleQuery();
    	RcRuleQuery.Criteria criteria = query.createCriteria();
    	criteria.andRuleNameEqualTo(name);
    	int count = rcRuleService.selectByExample(query).size();
    	return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), count);
    }

}
